package com.jwd.launch.service;

import java.util.ArrayList;
import java.util.List;

import com.AnywheeBt.SzhBAction;
import com.AnywheeBt.bean.BtContact;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class BluetoothService extends Service {

	private static final String TAG = "JK_BluetoothService";
	
	@Override
	public void onCreate() {
		Log.e(TAG, "----onCreate()----");
		super.onCreate();
    }

	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "----onStartCommand()----");
        return super.onStartCommand(intent, flags, startId);
    }
	
	@Override
	public IBinder onBind(Intent arg0) {
		Log.e(TAG, "----onBind()----");
		return mBluetoothService;
	}
	
	@Override
    public void onRebind(Intent intent) {
        Log.v(TAG, "----onRebind()----");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.v(TAG, "----onUnbind()----");
        return false;
    }

	IBluetoothService.Stub mBluetoothService = new IBluetoothService.Stub() {
		@Override
		public List<PeopleParcelable> queryPeoplesByName(String name, String s)
				throws RemoteException {
			Log.e(TAG, "----queryPeoplesByName()----" + name + "    " + s);
			List<BtContact> mBtContacts = SzhBAction.queryPeoplesByName(name, s);
			List<PeopleParcelable> mPeoples = new ArrayList<PeopleParcelable>();
			for(BtContact contact : mBtContacts) {
				PeopleParcelable people = new PeopleParcelable();
				people.name = contact.getName();
				people.phonenum = contact.getNumber();
				mPeoples.add(people);
			}
			return mPeoples;
		}
		
		@Override
		public void phoneHangUp() throws RemoteException {
			Log.e(TAG, "----phoneHangUp()----");
		}
		
		@Override
		public boolean phoneCall(String number) throws RemoteException {
			Log.e(TAG, "----phoneCall()----");
			int result = SzhBAction.CallPhone(number);
			return result == -1 ? false : true;
		}
		
		@Override
		public void phoneAnswer() throws RemoteException {
			Log.e(TAG, "----phoneAnswer()----");
		}

		@Override
		public boolean isBtConnected() throws RemoteException {
			Log.e(TAG, "----isBtConnected()----");
			Log.e(TAG, "state:" + SzhBAction.GetBtState());
			return SzhBAction.GetBtState();
		}
	};
}
